package com.sskj.hhrt.transaction;


import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;

import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadMoreListener;
import com.sskj.hhrt.MVPActivity;
import com.sskj.base.adapter.BaseAdapter;
import com.sskj.base.adapter.ViewHolder;
import com.sskj.base.view.DividerLineItemDecoration;
import com.sskj.base.view.ToolBarLayout;
import com.sskj.hhrt.R;
import com.sskj.hhrt.transaction.data.OrderHistoryBean;

import butterknife.BindView;

/**
 * @author Hey
 * Create at  2019/03/21
 */
public class TodayEntrustActivity extends MVPActivity<TodayEntrustPresenter> {
    @BindView(R.id.toolbar)
    ToolBarLayout toolbar;
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    @BindView(R.id.smartRefreshLayout)
    SmartRefreshLayout smartRefreshLayout;
    private BaseAdapter<OrderHistoryBean.OrderHistory> baseAdapter;

    private int page = 1;
    private int pageSize = 10;

    @Override
    public int getLayoutId() {
        return R.layout.activity_today_entrust;
    }

    @Override
    public TodayEntrustPresenter getPresenter() {
        return new TodayEntrustPresenter();
    }

    @Override
    public void initView() {
        initRefreshLayout(smartRefreshLayout);
        toolbar.setLeftButtonOnClickListener(view -> finish());
        toolbar.setTitle("当日委托");
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        recyclerView.addItemDecoration(new DividerLineItemDecoration(this)
                .setFirstDraw(false)
                .setPaintWidth(20)
                .setDividerColor(Color.parseColor("#00ffffff"))
        );
        baseAdapter = new BaseAdapter<OrderHistoryBean.OrderHistory>(R.layout.item_entrust, null, recyclerView, true) {
            @Override
            public void bind(ViewHolder holder, OrderHistoryBean.OrderHistory item) {
                holder.setText(R.id.tv_type, item.getBuyBillType() == 1 ? "做多" : "做空")
                        .setText(R.id.tv_name, item.getStockName())
                        .setText(R.id.tv_money_type, item.getTradingType() == 0 ? "建仓" : "平仓")
                        .setText(R.id.tv_order_amount, item.getEntrustCount() + "")
                        .setText(R.id.tv_entrust_price, item.getTradingType() == 0 ? item.getPrice() : "市价")
                        .setText(R.id.tv_deal_amount, item.getEntrustSuccessCount() + "")
                        .setText(R.id.tv_repeal_amount, "0")
                        .setText(R.id.tv_date, item.getCreateTime());
                if (item.getBuyBillType() == 1) {
                    holder.setTextColor(R.id.tv_type, color(R.color.red));
                } else {
                    holder.setTextColor(R.id.tv_type, color(R.color.green));
                }
                switch (item.getBillStatus()) {
                    //已接收
                    case 1:
                        holder.setText(R.id.tv_msg, "已接收");
                        break;
                    //待成交
                    case 2:
                        holder.setText(R.id.tv_msg, "待成交");
                        break;
                    //已取消
                    case 3:
                        holder.setText(R.id.tv_msg, "已撤单")
                                .setText(R.id.tv_repeal_amount, item.getEntrustCount() + "");
                        break;
                    //已成交
                    case 4:
                        holder.setText(R.id.tv_msg, "已成交");
                        break;
                    //已作废
                    case 5:
                        holder.setText(R.id.tv_msg, "已作废");
                        break;
                }

            }
        };

    }

    @Override
    public void initData() {
        mPresenter.getTodayEntrust(account, page, pageSize);
        smartRefreshLayout.setOnRefreshLoadMoreListener(new OnRefreshLoadMoreListener() {
            @Override
            public void onLoadMore(RefreshLayout refreshLayout) {
                page++;
                mPresenter.getTodayEntrust(account, page, pageSize);
            }

            @Override
            public void onRefresh(RefreshLayout refreshLayout) {
                page = 1;
                mPresenter.getTodayEntrust(account, page, pageSize);
            }
        });
    }


    public void finishRefresh() {
        smartRefreshLayout.finishRefresh();
        smartRefreshLayout.finishLoadMore();
    }

    public static void start(Context context) {
        Intent intent = new Intent(context, TodayEntrustActivity.class);
        context.startActivity(intent);
    }

    public void setData(OrderHistoryBean data) {
        if (data != null) {
            if (!data.isHasNextPage()) {
                smartRefreshLayout.setNoMoreData(true);
            }
            if (page == 1) {
                baseAdapter.setNewData(data.getList());
            } else {
                baseAdapter.addData(data.getList());
            }
        } else {
            baseAdapter.setNewData(null);
        }
    }
}
